<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="spuId" prop="spuId">
        <el-input
          v-model="queryParams.spuId"
          placeholder="请输入spuId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名字" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入商品名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员id" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员昵称" prop="memberNickName">
        <el-input
          v-model="queryParams.memberNickName"
          placeholder="请输入会员昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:spuComment:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="skuId" align="center" prop="skuId" />
      <el-table-column label="spuId" align="center" prop="spuId" />
      <el-table-column label="商品名字" align="center" prop="spuName" />
      <el-table-column label="会员id" align="center" prop="memberId" />
      <el-table-column label="会员昵称" align="center" prop="memberNickName" />
      <el-table-column label="用户头像" align="center" prop="memberIcon" />
      <el-table-column label="星级" align="center" prop="star" />
<!--      <el-table-column label="点赞数" align="center" prop="likesCount" />-->
<!--      <el-table-column label="回复数" align="center" prop="replyCount" />-->
      <!-- <el-table-column
        label="评论图片/视频[json数据；[{type:文件类型,url:资源路径}]]"
        align="center"
        prop="resources"
      /> -->
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            icon="Edit"
            @click="handleReply(scope.row)"
            v-hasPermi="['product:spuComment:edit']"
            >回复</el-button
          >
          <el-button
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:spuComment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 回复 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="商品名称" :span="2">
          {{ form.spuName }}
        </el-descriptions-item>
        <el-descriptions-item label="用户" :span="2">{{
          form.memberNickName
        }}</el-descriptions-item>
        <el-descriptions-item label="内容" :span="2">
          {{ form.content }}
        </el-descriptions-item>
        <el-descriptions-item label="图片/视频" :span="2">
          <MPreview :fileList="form.resources" />
        </el-descriptions-item>
        <el-descriptions-item label="回复">
          <el-input type="textarea" v-model="replyForm.content"></el-input>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listSpuComment,
  getSpuComment,
  delSpuComment,
  addReply,
  updateReply,
  delReply,
} from "@/api/mall/server/order/comment";
import MPreview from "@/components-mall/MPreview";
const { proxy } = getCurrentInstance();
const { show_status } = proxy.useDict("show_status");
const list = ref([]);
const open = ref(false);
const infoOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const replyForm = ref({});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listSpuComment(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  open.value = false;
  reset();
};
/** 表单重置 */
const reset = () => {
  form.value = {
    id: undefined,
    skuId: undefined,
    spuId: undefined,
    spuName: undefined,
    content: undefined,
  };
  replyForm.value = {
    content: undefined,
  };
  proxy.resetForm("formRef");
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  open.value = true;
  title.value = "添加回复";
};
/**回复按钮操作 */
const handleReply = async (row) => {
  reset();
  const id = row.id || ids.value;
  const res = await getSpuComment(id);
  form.value = res.data;
  form.value.resources = JSON.parse(form.value.resources);
  replyForm.value.commentId = row.id;
  open.value = true;
  title.value = "回复";
};
/** 提交按钮 */
const submitForm = async () => {
  if (replyForm.value.content == "") {
    return proxy.$modal.msgError("请输入回复内容");
  }
  const res = await addReply(replyForm.value);
  proxy.$modal.msgSuccess("回复成功");
  open.value = false;
};
/** 删除按钮操作 */
const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + ids + '"的数据项？')
    .then(function () {
      return delSpuComment(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

getList();
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-top: 10px;
}
</style>
